﻿@model IEnumerable <Superstore.Vender.Models.ShoppingCartGoodsInfo>

<section class="ShopPart1">
    <div class="ShoppingTxt fll">
        <div class="ShoppingCar">
            <img src="/images/shopping_normal.png">
        </div>
        <div class="ShoppingMoney">
            <span id="ShoppingHelper">购物车是空的</span>
        </div>
    </div>
</section>

<section class="ShopPart2">
    <div class="ShoppingTxt fll">
        <div class="ShoppingCar" onclick="showShoppingDetail()">
            <img src="/images/shopping_normal.png">
            <span id="ShoppingCarTotalNum1"></span>
        </div>
        <div class="ShoppingMoney">
            <span>共</span><a id="ShoppingCarTotalPrice1"></a>
        </div>
    </div>
    <div class="ShoppingAll flr" onclick="prePay()">
        <a><span>去结算</span></a>
    </div>
</section>

<section class="ShopPart3">
    <section class="ShopCar">
        <div class="ShopCarTop">
            <div class="ShoppingCar">
                <img src="/images/shopping_normal.png">
                <span id="ShoppingCarTotalNum2"></span>
            </div>
            <a onclick="resetShopping()">清空购物车</a>
        </div>
        <div class="ShopCarCon">
            <div class="goodsListDoc">
                <table id="table_goodsList"></table>
            </div>
        </div>
    </section>
    <div style="overflow:hidden;">
        <div class="ShoppingTxt fll" onclick="hideShoppingCar()">
            <div class="ShoppingMoney">
                <span>共</span><a id="ShoppingCarTotalPrice2"></a>
            </div>
        </div>
        <div class="ShoppingAll fll" onclick="prePay()">
            <a><span>去结算</span></a>
        </div>
    </div>
</section>

<script>
    $(function () {
        adjustShoppingInfo();
        loadShoppingCarGoodsList();
    });
    function showShoppingCar() {
        $('.OrderGray').show();
        $('.Shopping').attr("style", "display:block;z-index:999;");
        $('body').css('overflow', 'hidden');
    }
    function hideShoppingCar() {
        $('.OrderGray').hide();
        adjustShoppingInfo();
        $('body').css('overflow', 'scroll');
    }
    function loadShoppingCarGoodsList() {
        $("table#table_goodsList tr").each(function () {
            $(this).remove();
        });
        var shoppingList = ShoppingMgr.getShoppingList();
        for (index in shoppingList) {
            if (shoppingList[index].goods_num > 0) {
                var tr = $(
                    '<tr id=\"tr_' + shoppingList[index].goods_info.goodsid + '\">' +
                    '<td class="shopping-goods-name">' + shoppingList[index].goods_info.name + '</td>' +
                    '<td class="shopping-goods-price">￥' + shoppingList[index].goods_info.price + '</td>' +
                    '<td class="shopping-goods-count"><div class=\"IndexGoodsNum\">' +
                    '<a class=\"dec_shoping_count_' + shoppingList[index].goods_info.goodsid + '\" onclick=\"adjustNum2(' + shoppingList[index].goods_info.goodsid + ',' + shoppingList[index].main_tab + ',' + shoppingList[index].sub_tab + ', -1)\">' + 
                    '<img src=\"/images/Dec.png\" ontouchstart="replaceAddOrDecImage(this, 2)" ontouchend="replaceAddOrDecImage(this, 4)" ></a>' +
                '<span id=\"shoping_goodsid_' + shoppingList[index].goods_info.goodsid + '\">' + shoppingList[index].goods_num + '</span>' +
                '<a class=\"add_shoping_count_' + shoppingList[index].goods_info.goodsid + '\" onclick=\"adjustNum2(' + shoppingList[index].goods_info.goodsid + ',' + shoppingList[index].main_tab + ',' + shoppingList[index].sub_tab + ', 1)\">' +
                '<img src=\"/images/Add.png\" ontouchstart="replaceAddOrDecImage(this, 1)" ontouchend="replaceAddOrDecImage(this, 3)" ></a>' +
                '</div></td></tr>');
                $("table#table_goodsList").append(tr);
            }
        }
    }

    function showShopPart1() {
        $('.ShopPart1').show();
        var flagFallPrice = TerminalMgr.getFlagFallPrice();
        $('#FlagFallPrice').text(flagFallPrice);
    }

    function hideShopPart1() {
        $('.ShopPart1').hide();
    }

    function showShopPart2() {
        var totalNum = ShoppingMgr.getTotalNum();
        var totalPrice = ShoppingMgr.getTotalPrice();
        $('.ShopPart2').show();
        $('#ShoppingCarTotalPrice1').html("￥" + totalPrice.toFixed(1));
        $('#ShoppingCarTotalNum1').text(totalNum);

    }

    function hideShopPart2() {
        $('.ShopPart2').hide();
    }

    function showShopPart3() {
        var totalNum = ShoppingMgr.getTotalNum();
        var totalPrice = ShoppingMgr.getTotalPrice();
        $('.ShopPart3').show();
        $('#ShoppingCarTotalPrice2').html("￥" + totalPrice.toFixed(1));
        $('#ShoppingCarTotalNum2').text(totalNum);

        loadShoppingCarGoodsList();
    }

    function hideShopPart3() {
        $('.ShopPart3').hide();
    }

    function adjustShoppingInfo() {
        $('.OrderGray').hide();
        var totalNum = ShoppingMgr.getTotalNum();
        if (totalNum == 0) {
            showShopPart1();
            hideShopPart2();
        } else {
            hideShopPart1();
            showShopPart2();
        }
        hideShopPart3();
    }

    function prePay() {
        var isLog = UserInfoMgr.isLogIn();
        if (isLog) {
            window.location.href = '/Buy/Index';
        } else {
            window.location.href = spliceRegisterUrl('/Buy/Index', '/Home/Index');
        }
    }

    function resetShopping() {
        ShoppingMgr.resetShoppingMgr();
        hideShopPart2();
        hideShopPart3();
        showShopPart1();

        $('.ShopCarCon table').empty();
        $('.ShoppingCar span').text(ShoppingMgr.getTotalNum());
        $('.IndexPart .IndexGoodsNum').each(function (index) {
            $('.IndexPart .IndexGoodsNum span').text('0');
            $('.IndexPart .IndexGoodsNum span').hide();
            $('.IndexPart .IndexGoodsNum a').eq(2 * index).hide();
        });

        hideShoppingCar();
    }

    function showShoppingDetail() {
        hideShopPart1();
        hideShopPart2();
        showShopPart3();
        showShoppingCar();
    }
    var StatusType;
    function adjustShopListShow(goodsid, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_shoping_count_" + goodsid).show();
            $("#shoping_goodsid_" + goodsid).show();
            $("#shoping_goodsid_" + goodsid).text(goodsNum);
        } else {
            $("#tr_" + goodsid).parent().remove();
            var totalNum = ShoppingMgr.getTotalNum();
            if (totalNum == 0) {
                showShopPart1();
                hideShopPart2();
                hideShopPart3();
                hideShoppingCar();
            }
            else {
                showShopPart3();
                hideShopPart1();
                hideShopPart2();
            }
        }
    }
    //函数名末尾的2是为了与商品展示页面中的函数名区分开来
    function adjustGoodsNumShow2(goodsid, goodsNum, shoppingList) {
        adjustGoodsListShow(goodsid, goodsNum);
        //adjustPromotionGoodsListShow(goodsid, goodsNum);
        adjustShopListShow(goodsid, goodsNum);
        var totalPrice = ShoppingMgr.getTotalPrice();
        $('#ShoppingCarTotalPrice1').html("￥" + totalPrice.toFixed(1));
        $('#ShoppingCarTotalPrice2').html("￥" + totalPrice.toFixed(1));
        $('.ShoppingCar span').text(ShoppingMgr.getTotalNum());
    }
    //函数名末尾的2是为了与商品展示页面中的函数名区分开来
    function adjustNum2(goodsId, mainTab, subTab, typeOp) {
        ShoppingMgr.adjustGoodsNum(goodsId, mainTab, subTab, typeOp, adjustGoodsNumShow2);
    }
</script>
